/**
 * @FileName: LogcatActivity.java
 * @Package com.hugh.systeminfo
 * @Description: 
 * @author hugh
 * @date 2013-9-26
 */
package com.hugh.systeminfo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 日志输出界面 
 * @author hugh
 * @date 2013-9-26
 */
public class LogcatActivity extends Activity{
    
    private static final String TAG = "LogcatActivity";
    
    private static final int MSG_LOG = 1;
    private static final int MSG_SCROLL = 2;
    
    private static final int TEXT_LOG_BUFFER = 10240;    //10k 日志
    
    private ScrollView mScroll;
    private TextView mLogView;
    
    private boolean mStop = false;
    
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
            case MSG_LOG:
                
                int length = mLogView.length() + ((String)msg.obj).length();
                if(length > TEXT_LOG_BUFFER){
                    mLogView.getEditableText()
                            .replace(0, length - TEXT_LOG_BUFFER, "")
                            .append((String) msg.obj);
                } else {
                    mLogView.append((String)msg.obj);
                }
                mScroll.fullScroll(View.FOCUS_DOWN);
                
                // find the amount we need to scroll.  This works by
                // asking the TextView's internal layout for the position
                // of the final line and then subtracting the TextView's height
//                final int scrollAmount = mLogView.getLayout().getLineTop(
//                        mLogView.getLineCount())
//                        - mLogView.getHeight();
                // if there is no need to scroll, scrollAmount will be <=0
//                if (scrollAmount > 0){
//                    mLogView.scrollTo(0, scrollAmount);
//                }
                break;
            case MSG_SCROLL:
                mScroll.fullScroll(View.FOCUS_DOWN);
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logcat);
        
        mScroll = (ScrollView) findViewById(R.id.sc_logcat);
        mLogView = (TextView) findViewById(R.id.tv_logcat);
//        mLogView.setMovementMethod(new ScrollingMovementMethod());
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        startLog();
    }



    @Override
    protected void onStop() {
        mStop = true;
        mHandler.removeMessages(MSG_LOG);
        super.onStop();
    }

    /**
     * 开始输出日志
     */
    private void startLog(){
        new Thread(){
            @Override
            public void run() {
                Process mLogcatProc = null;
                BufferedReader reader = null;
                try {
                    //获取logcat日志信息
                    mLogcatProc = Runtime.getRuntime().exec("logcat *:D");
                    reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));
        
                    Log.i(TAG ,"log read thread start");
                    String line;
                    while (true) {
                        if((line = reader.readLine()) != null && !mStop){
                            // 使用handler 把日志给界面
                            Message m = mHandler.obtainMessage(MSG_LOG, line + " \n");
                            mHandler.sendMessage(m);
                        } else {
                            if(mStop){
                                break;
                            }
//                            mHandler.sendEmptyMessageDelayed(MSG_SCROLL, 50);
                            Thread.sleep(3000);
//                            reader = new BufferedReader(new InputStreamReader(
//                                    mLogcatProc.getInputStream()));
                        }
                    }
                    Log.i(TAG ,"log read thread stoped");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
